//
//  HMOAuthViewController.m
//  HMWeiBoOC
//
//  Created by GD on 16/9/20.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "HMOAuthViewController.h"
#import "UIBarButtonItem+Extention.h"
#import "HMNetworkTools.h"
#import "HMUserAccount.h"
#import "HMUserAccountViewModel.h"
#import <SVProgressHUD.h>
#import "HMGlobalConst.h"

//NSString *WeiboAppKey = @"2707543009";
//
//NSString *WeiboRedirect_Uri = @"http://www.itcast.cn";

@interface HMOAuthViewController ()<UIWebViewDelegate>

@property (nonatomic,strong) UIWebView *webView;

@end

@implementation HMOAuthViewController

-(UIWebView *)webView{
    if (_webView == nil) {
        _webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _webView.delegate = self;
    }
    return _webView;
}

-(void)loadView{
    self.view = _webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self requestLogin];
    
    [self setupNavUI];
}

-(void)requestLogin{
    NSString *url = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",WeiboAppKey,WeiboRedirect_Uri];
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}

-(void)setupNavUI{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initwithTitle:@"取消" imageName:nil target:self action:@selector(cancelAciton)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initwithTitle:@"自动填充" imageName:nil target:self action:@selector(autoFillAciton)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    self.title = @"微博";
}

-(void)autoFillAciton{
//    [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('userId').value = '15300230977';document.getElementById('passwd').value = '2012213576bss'"];
     [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('userId').value = '15901458105';document.getElementById('passwd').value = 's080920'"];
}

-(void)cancelAciton{
    [self dismissViewControllerAnimated:YES completion:nil];
}
    
    
#pragma mark -UIWebViewDelegate
-(void)webViewDidStartLoad:(UIWebView *)webView{
    [SVProgressHUD show];
}
    
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [SVProgressHUD dismiss];
}
    
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [SVProgressHUD dismiss];
}
    
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    if ([request.URL.query hasPrefix:@"code="]) {
        NSString *code = [request.URL.query substringFromIndex:@"code=".length];
        
        //通过code 获取access_Token
        NSLog(@"%@  %@",request.URL.query,code);
        [[HMUserAccountViewModel sharedViewModel] requestAccessToken:code callBack:^(BOOL isSuccess) {
            //  要等到dismis完成以后在发送切换根视图控制器的操作
            if (isSuccess) {
               [ self dismissViewControllerAnimated:NO completion:^{
                   [[NSNotificationCenter defaultCenter] postNotificationName:SwitchRootVCNotification object:self ];
               }];
            }
        }];
        
        //判断登录是否成功,
//        return NO;
    }else{//取消授权
//        [self dismissViewControllerAnimated:YES completion:nil];
    }

    return YES;
}
    
    

@end
